import requests
import pytest
import json
import sys,os,time
import allure
# sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# sys.path.append((os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))))
# print((os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))))
# time.sleep(20)
sys.path.append(os.path.split(os.path.split(os.path.abspath("__name__"))[0])[0])
from common1.set_data import Setdata
from common1.read_log import Mylog
from common1.doexcel import DoExcel
from common1.http_request import Http_Request
# url='http://frs.hang-shu.com:10083/oauth/token'
# header={"Authorization": "Basic eXotd2ViOnNyaF9tYW51ZmFjdHVyZV95el85OTk=="}
# data={"grant_type": "password", "username": "yz_yz", "password": "123456"}
# f=requests.post(url=url,data=data,headers=header)
# print(f.json())
# @pytest.mark.parametrize('name,age,addr',[("a","b","c"),("aa","bb","cc"),("a2","b2","c2")])
# @pytest.mark.parametrize('name,age,addr',[{"a":11,"b":22,"c":33},{"a":33,"b":44,"c":55}])
log=Mylog()
data_list=DoExcel().doex()
print(data_list)
print(len(data_list))


@allure.feature("YZ接口自动化测试")
class Test_yz:
    @pytest.mark.parametrize('inData',data_list)
    def test_getdata(self,inData):
        allure.dynamic.story(inData["desc"])
        allure.dynamic.title(inData["desc"])
        allure.dynamic.description(inData["desc"])
        print("***********************************")
        # print(inData)
        if inData["header"].find("${Token}") != -1:
            inData["header"] = inData["header"].replace("${Token}", str(getattr(Setdata, "token")))

        res=Http_Request().httpre(inData["http_method"],inData["url"],eval(inData["header"]),eval(inData['Data']))
        try:
            setattr(Setdata,"token","Bearer "+ res.json()['value'])
        except Exception as e:
            pass
        try:
            assert(inData['expected'], res.status_code, '前后数据不一致，测试失败')
            result = "Pass"
            log.info("测试成功===》测试用例名：{}".format(inData['desc']))
        except Exception as e:
            log.error(e)
            result = "Fail"
            raise

        finally:
            DoExcel().write_back(inData["caseId"], res.status_code, result)

if __name__ == '__main__':
    pytest.main(["test1109.py","-s","--alluredir","../report/reportallure"])
    os.system("allure generate ../report/reportallure/ -o ../reporthtml/ --clean")



